package com.github.common.extend.demo.util.excel.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.time.LocalDateTime;

/**
 * @author wangjj7
 * @date 2022/5/13
 * @description
 */
public class AdaptiveLocalDateTimeConverter implements Converter<LocalDateTime> {

    @Override
    public Class supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public LocalDateTime convertToJavaData(ReadCellData cellData,
                                           ExcelContentProperty excelContentProperty,
                                           GlobalConfiguration globalConfiguration)
    {
        return CellUtils.cellDate2LocalDateTime(cellData ,excelContentProperty);
    }

    @Override
    public WriteCellData convertToExcelData(LocalDateTime localDateTime,
                                            ExcelContentProperty excelContentProperty,
                                            GlobalConfiguration globalConfiguration)
    {
        if (localDateTime == null){
            return new WriteCellData("");
        }
        return new WriteCellData(CellUtils.localDateTime2Number(localDateTime));
    }

}
